<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
$intGID = (int)$_data["GambleID"];
$strBT = trim($_data["Bet_Type"]);
$isSpecial = (strlen($_data["isSpecial"]) == 0)?1:(int)$_data["isSpecial"];
$isRealShow = (strlen($_data["isRealShow"]) == 0)?1:(int)$_data["isRealShow"];

$intSpecialNumType = (int)$_data["SpecialNumType"];
$arrBType = explode(",", $strBT);
$arrOtherMsg = explode(",", trim($_data["OtherMsg"]));
$strLottoNumber = trim($_data["strLottoNumber"]);

$strWagerType = "-1";
$intUserID = $_SESSION["Admin_Info"] -> ID;
switch($arrPower[1]){
	case "all":
		$strSeach = " ";
		$strSeach2 = " ";
		$strMulti = ($isRealShow == 1)?"*(_DirectorRate)":"";
		$strComm = ($isRealShow == 1)?"*(_DirectorCommission)":"*0";
		$strWagerType = "0,1,2,3,4,5";
		$strSWType = "-999";
		break;

	case "director-sub":
		$intUserID = $_SESSION["Admin_Info"] -> ParentID;
	case "director":
		if($isRealShow == 2 && $_SESSION["Admin_Info"] -> isAllAccount == 1){	//公司總帳
			$strSeach = " ";
			$strSeach2 = " ";
			$strMulti = "*(_DirectorRate)";
			$strComm = "*(_DirectorCommission)";
			$strWagerType = "0,1,2,3,4,5";
			$strSWType = "-999";
		}else{
			//if(empty($intDirectorID))$intDirectorID = $_SESSION["Admin_Info"] -> ID;
			$strSeach = " and DirectorID = ".$intUserID." ";
			$strSeach2 = " and b.DirectorID = ".$intUserID." ";
			$strMulti = ($isRealShow == 1)?"*(_StockRate - _DirectorRate)":"";
			$strComm = ($isRealShow == 1)?"*_StockCommission":"*0";
			$strWagerType = "0,2,3,4,5";
			$strSWType = "5";
		}
		break;

	case "stock-sub":
		$intUserID = $_SESSION["Admin_Info"] -> ParentID;
	case "stock":
		/*if($isRealShow == 2 && $_SESSION["Admin_Info"] -> isAllAccount == 1){	//總監總帳
			if($_SESSION["Admin_Info"] -> ParentisAllAccount == 1){
				$strSeach = " ";
				$strSeach2 = " ";
				$strMulti = "*(_DirectorRate)";
				$strComm = "*(_DirectorCommission)";
				$strWagerType = "0,1,2,3,4";
				$strSWType = "-999";
			}else{
				$strSeach = " and DirectorID = ".$_SESSION["Admin_Info"] -> ParentID." ";
				$strSeach2 = " and b.DirectorID = ".$_SESSION["Admin_Info"] -> ParentID." ";
				$strMulti = "*(_DirectorRate)";
				$strComm = "*(_DirectorCommission)";
				$strWagerType = "0,1,2,3,4";
				$strSWType = "-999";
			}
		}else{*/
			$strSeach = " and StockID = ".$intUserID." ";
			$strSeach2 = " and b.StockID = ".$intUserID." ";
			$strMulti = ($isRealShow == 1)?"*(_MainRate - _StockRate)":"";
			$strComm = ($isRealShow == 1)?"*_MainCommission":"*0";
			$strWagerType = "0,2,3,4";
			$strSWType = "4";
		//}
		break;

	case "main-sub":
		$intUserID = $_SESSION["Admin_Info"] -> ParentID;
	case "main":
		$strSeach = " and MainID = ".$intUserID." ";
		$strSeach2 = " and b.MainID = ".$intUserID." ";
		$strMulti = ($isRealShow == 1)?"*(_SubRate - _MainRate)":"";
		$strComm = ($isRealShow == 1)?"*_SubCommission":"*0";
		$strWagerType = "0,2,3";
		$strSWType = "3";
		break;

	case "sub-sub":
		$intUserID = $_SESSION["Admin_Info"] -> ParentID;
	case "sub";
		$strSeach = " and SubID = ".$intUserID." ";
		$strSeach2 = " and b.SubID = ".$intUserID." ";
		$strMulti = ($isRealShow == 1)?"*(1 - _SubRate)":"";
		$strComm = ($isRealShow == 1)?"*_MemCommission":"*0";
		$strWagerType = "0,2";
		$strSWType = "2";
		break;
}

$arrSumStake = array();
$arrSumPayOff = array();
$arrSumCommission = array();
$arr92Sort = array(92, 93);
if(preg_match("/69|73/", $strBT)){ //組三轉直  組六轉直
	$strMulti2 = str_replace('_', 'b.', $strMulti);
	$strComm2 = str_replace('_', 'b.', $strComm);

	$intDenominator = (preg_match("/69/", $strBT))?64:69;///(a.Bet_Type-".$intDenominator.")

	$strSQL = "select a.Bet_Type as Bet_Type, a.Number as Number, Count(*) as total, sum(IF(b.WagerType = ".$strSWType.",b.MemStake*-1,b.MemStake".$strMulti2.")*b.MemExchangeRate) as betstatus, sum(IF(WagerType = ".$strSWType.",b.MemPayOff*-1,b.MemPayOff".$strMulti2.")*b.MemExchangeRate) as paystatus,  sum(IF(WagerType = ".$strSWType.",b.MemStake*-1,b.MemStake".$strMulti2.")*b.MemExchangeRate".$strComm2.") as commstatus  from `bet_ParlaysNumber` a, `bet_BetListTmp` b where a.BetListID = b.ID and b.GambleID = ".$intGID.$strSeach2." and  b.Bet_Type in (".$strBT.") group by a.Bet_Type, a.Number";

}elseif(preg_match("/92|93/", $strBT)){ //三定位
	$intBT = (int)$strBT;

	$strMulti = str_replace('_', '', $strMulti);
	$strComm = str_replace('_', '', $strComm);
	$strSumSQL = "select Count(*) as total, sum(IF(WagerType = ".$strSWType.",MemStake*-1,MemStake".$strMulti.")*MemExchangeRate) as betstatus, sum(IF(WagerType = ".$strSWType.",MemPayOff*-1,MemPayOff".$strMulti.")*MemExchangeRate) as paystatus,  sum(IF(WagerType = ".$strSWType.",MemStake*-1,MemStake".$strMulti.")*MemExchangeRate".$strComm.") as commstatus from `bet_BetListTmp` where Bet_Type in (".join(",", $arrBType).") and GambleID = ".$intGID.$strSeach." and isPayOff >= 0 and WagerType in (".$strWagerType.") and Bet_Type = '".$strBT."'";

	if(!$RS = PMA_mysql_query($strSumSQL, $$_Conn)){
		$GLOBALS["errormessage"] = "Find TodayGame Data Error!!";
		$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
		ErrMsgOutputHTML("Error", "", 1);
	}
	$Rows = PMA_mysql_fetch_object($RS);
	$arrSumStake[$intBT] = $Rows -> betstatus;
	$arrSumPayOff[$intBT] = $Rows -> paystatus;
	$arrSumCommission[$intBT] = $Rows -> commstatus;

	$strSQL = "select Bet_Type, Number, Count(*) as total, sum(IF(WagerType = ".$strSWType.",MemStake*-1,MemStake".$strMulti.")*MemExchangeRate) as betstatus, sum(IF(WagerType = ".$strSWType.",MemPayOff*-1,MemPayOff".$strMulti.")*MemExchangeRate) as paystatus,  sum(IF(WagerType = ".$strSWType.",MemStake*-1,MemStake".$strMulti.")*MemExchangeRate".$strComm.") as commstatus, (".($Rows -> tolStake - $Rows -> tolCommission)." - sum(IF(WagerType = ".$strSWType.",MemStake*-1,MemStake".$strMulti.")*MemExchangeRate) - sum(IF(WagerType = ".$strSWType.",MemPayOff*-1,MemPayOff".$strMulti.")*MemExchangeRate)) as preWin from `bet_BetListTmp` where Bet_Type in (".join(",", $arrBType).") and GambleID = ".$intGID.$strSeach." and isPayOff >= 0 and WagerType in (".$strWagerType.") ";
	switch($arrOtherMsg[0]){
		case "Sort": //排序
			$strSQL .= "group by Bet_Type, Number having preWin <= 0 order by preWin, Number limit ";
			$strSQL .= ($strBT != "93")?"100":"220";
			break;

		default:
			$strSQL .= "and Number in (".$strLottoNumber.") group by Bet_Type, Number order by Number";
			break;
	}

}else{
	$strMulti = str_replace('_', '', $strMulti);
	$strComm = str_replace('_', '', $strComm);
	$strSQL = "select Bet_Type, Number, Count(*) as total, sum(IF(WagerType = ".$strSWType.",MemStake*-1,MemStake".$strMulti.")*MemExchangeRate) as betstatus, sum(IF(WagerType = ".$strSWType.",MemPayOff*-1,MemPayOff".$strMulti.")*MemExchangeRate) as paystatus,  sum(IF(WagerType = ".$strSWType.",MemStake*-1,MemStake".$strMulti.")*MemExchangeRate".$strComm.") as commstatus from `bet_BetListTmp` where Bet_Type in (".join(",", $arrBType).") and GambleID = ".$intGID.$strSeach." and isPayOff >= 0 and WagerType in (".$strWagerType.") group by Bet_Type, Number";
} //if($strBT == "15")//
//print $strSQL;

if(preg_match("/68/", $strBT)){
	$strMulti = str_replace('_', '', $strMulti);
	$strComm = str_replace('_', '', $strComm);
	$strSQL .= " union (select Bet_Type, Number, Count(*) as total, sum(IF(WagerType = ".$strSWType.",MemStake*-1,MemStake".$strMulti.")*MemExchangeRate) as betstatus, sum(IF(WagerType = ".$strSWType.",MemPayOff*-1,MemPayOff".$strMulti.")*MemExchangeRate) as paystatus,  sum(IF(WagerType = ".$strSWType.",MemStake*-1,MemStake".$strMulti.")*MemExchangeRate".$strComm.") as commstatus from `bet_BetListTmp` where Bet_Type in (68) and GambleID = ".$intGID.$strSeach." and isPayOff >= 0 and WagerType in (".$strWagerType.") group by Bet_Type, Number) ";
}

if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errormessage"] = "Find Wager Data Error!!";
	$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
	print $GLOBALS["errorhidden"];
	ErrMsgOutputHTML("Error", "", 1);
}

$arrNum92Sort = array();
$arrNum = array();
while($Rows = PMA_mysql_fetch_object($RS)){
	$arrWCount[$intGID][$Rows -> Bet_Type][$Rows -> Number] = $Rows -> total;
	$arrWSum[$intGID][$Rows -> Bet_Type][$Rows -> Number] = $Rows -> betstatus;
	$arrPayOff[$intGID][$Rows -> Bet_Type][$Rows -> Number] = $Rows -> paystatus;
	$arrCommission[$intGID][$Rows -> Bet_Type][$Rows -> Number] = $Rows -> commstatus;
	$arrNum92Sort[$Rows -> Number] = true;
	$arrNum[] = $Rows -> Number;
}
if(in_array($intBT, $arr92Sort)){
	if($arrOtherMsg[0] == "Sort"){ //三定位
		if($intBT != 93){
			$i = 0;
			while(sizeof($arrNum92Sort) < 100){
				if(!$arrNum92Sort[$i]){
					$arrNum92Sort[$i] = true;
					$arrNum[] = $i;
				}
				$i++;
			}
			$arrTodayGame = getTodayGameDataForMultiNumber($intLottoID, $intGID, $intBT, join(",", $arrNum));
		}else{
			$arrLottoNumber = explode(",", $strLottoNumber);
			foreach($arrLottoNumber as $intLNum){
				if(!$arrNum92Sort[$intLNum]){
					$arrNum92Sort[$intLNum] = true;
				}
			}
			$arrTodayGame = getTodayGameDataForMultiNumber($intLottoID, $intGID, $intBT, $strLottoNumber);
		}
	}else{
		$arrTodayGame = getTodayGameDataForMultiNumber($intLottoID, $intGID, $intBT, $strLottoNumber);
	}
	if($arrTodayGame === false)ErrMsgOutputHTML("Error", "", 1);
}else{
	for($i = 0; $i < sizeof($arrBType); $i++)include(__Common_Path."/todaygame/TodayGame".$intLottoID."_".$arrBType[$i].".inc.php");
	$arrTodayGame = $_arrTodayGame[$intGID];
}

$intTmpBT = 0;
$intNo = -1;
if(is_array($arrTodayGame)){
	foreach($arrTodayGame as $intBT => $arrN){
		$arrTmpNum = (in_array($intBT, $arr92Sort) && $arrOtherMsg[0] == "Sort")?$arrNum92Sort:$arrN;

		foreach($arrTmpNum as $intNumber => $v){
			$Rows = $arrTodayGame[$intBT][$intNumber];
			if(in_array($intBT, $arr92Sort) && $arrOtherMsg[0] != "Sort" && !preg_match("/,".$Rows["Number"].",/", ",".$strLottoNumber.","))continue;

			$intWCount = $arrWCount[$Rows["GambleID"]][$intBT][$Rows["Number"]]+0;
			$intWSum = $arrWSum[$Rows["GambleID"]][$intBT][$Rows["Number"]]+0;
			$intPayOff = $arrPayOff[$Rows["GambleID"]][$intBT][$Rows["Number"]]+0;
			$intCommission = $arrCommission[$Rows["GambleID"]][$intBT][$Rows["Number"]]+0;

			$intDenominator = 1;
			if($intBT >= 69 && $intBT <= 77){
				if($intBT <= 72){$intDenominator = $intBT - 64;}
				else{$intDenominator = $intBT - 69;}

				//$intWCount = $intWCount / $intDenominator;
				$intWSum = $intWSum / $intDenominator;
				$intPayOff = $intPayOff / $intDenominator;
				$intCommission = $intCommission / $intDenominator;
			}

			$intApointNum = $_arrGLineNumToApointNum[$Rows["LottoID"]][$Rows["Bet_Type"]][$Rows["Number"]];
			if($intApointNum == null)$intApointNum = 0;
			$flGLine = $_arrGambleType[$Rows["Bet_Type"]][$intApointNum];
			
			$intNo = (in_array((int)$Rows["Bet_Type"], $arr92Sort))?($intNo+1):$Rows["Number"];

			$strCon .= "parent.Game".$Rows["Bet_Type"]."[".$intNo."]=new Array('".$Rows["LottoID"]."','".$Rows["GambleID"]."','".$Rows["Bet_Type"]."','".$Rows["Number"]."','".$Rows["OddsID"]."','".($Rows["Odds"]+$flGLine)."','".$Rows["isStopBet"]."','".$intWCount."','".$intWSum."','".$intPayOff."','".$intCommission."','".$intSelfStop."');\n";

			switch($Rows["Bet_Type"]){
				case 92:
				case 93:
					break;

				default:
					$arrSumStake[$Rows["Bet_Type"]] += $intWSum;
					$arrSumPayOff[$Rows["Bet_Type"]] += $intPayOff;
					$arrSumCommission[$Rows["Bet_Type"]] += $intCommission;
					break;

			} //switch($Rows["Bet_Type"])
		} //foreach($arrTmpNum as $intNumber => $v)
	} //foreach($arrTodayGame as $intBID => $arrN)
} //if(is_array($arrTodayGame))


/*=================================================================*/
header("Cache-Control: no-store, no-cache, must-revalidate");	  // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");															  // HTTP/1.0
?><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="JavaScript">
<!--
<?php 
print $strCon."\n";

foreach($arrSumStake as $intBT => $val){
	print "parent.sumStake[".$intBT."] = ".round($arrSumStake[$intBT]).";\n";
	print "parent.sumPayOff[".$intBT."] = ".round($arrSumPayOff[$intBT]).";\n";
	print "parent.sumCommission[".$intBT."] = ".round($arrSumCommission[$intBT]).";\n";
}
switch($strBT){
	case "51";
	case "52";
	case "53";
	case "54";
	case "55";
	case "56";
	case "57";
	case "58";
	case "59";
	case "60";
	case "61";
	case "62";
	case "63";
	case "84";
	case "85";
	case "86";
	case "51,52,53,54,55,56,57,58,59,60,61,62,63,84,85,86":
		print "parent.ShowNumberOdds51(51);\n";
		break;

	//雙飛
	case "64";
	case "65";
	case "66";
	case "67";
	case "64,65,66";
		print "parent.ShowTwinNumer(".$strBT.");\n";
		break;

	//組 3 轉直
	case "68";
	case "69";
	case "70";
	case "71";
	case "72";
	case "68,69,70,71,72":
		print "parent.ShowAssembleNumber(68);\n";
		break;

	//組 6 轉直
	case "73";
	case "74";
	case "75";
	case "76";
	case "77";
	case "73,74,75,76,77":
		print "parent.ShowAssembleNumber(73);\n";
		break;

	//3x3x3
	case "78";
	case "79";
	case "80";
	case "81";
	case "82";
	case "83";
	case "78,79,80,81,82,83":
		print "parent.ShowRowsNumber(78);\n";
		break;

	//跨度過關
	case "87";
	case "88";
	case "89";
	case "90";
	case "91";
	case "87,88,89,90,91":
		print "parent.ShowOPNumber(87);\n";
		break;

	//三定位
	case "92";
		print "parent.ShowTrinityNumber(92);\n";
		break;

	//三字
	case "93":
		print "parent.Show3WordNumber(93);\n";
		break;
} //switch($strBT)
?>
//-->
</script>